/*
 * LocalFileSystem.java
 *
 * Created on December 2, 2006, 11:39 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package eu.somatik.somaftp.logic.local;

import com.glub.secureftp.bean.RemoteFile;
import eu.somatik.somaftp.SomaFTP;
import eu.somatik.somaftp.logic.*;
import eu.somatik.somaftp.logic.AbstractFileSystem;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.util.LinkedList;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.ListModel;

/**
 *
 * @author francisdb
 */
public class LocalFileSystem extends AbstractFileSystem<LocalFile>{
    
    private File curDir;
    
    private static final int BUFFERSIZE = 1024*64;
    
    /** Creates a new instance of LocalFileSystem */
    public LocalFileSystem(SomaFTP somaFTP) {
        super(somaFTP, LocalFile.class);
        curDir = new File(System.getProperty("user.dir"));
    }
    
    public void changeDirectory(LocalFile dir) {
        curDir = dir.getFileObject();
    }
    
    public void connect(final DefaultListModel logModel) {
    }
    
    public boolean isConnected() {
        // always connected
        return true;
    }
    
    public void disconnect() {
    }
    
    public String getPath() {
        return curDir.getAbsolutePath();
    }
    
    public List<FileInterface> list(boolean includeParent) {
        List<FileInterface> list = null;
        File[] files = curDir.listFiles();
        list = new LinkedList<FileInterface>();
        if(includeParent){
            File parent = curDir.getParentFile();
            if(parent != null){
                LocalFile parentFile = new LocalFile(parent, true, this);
                list.add(parentFile);
            }
        }
        for(File file:files){
            list.add(new LocalFile(file, this));
        }
        return list;
    }
    
    public void receive(FileTransfer transfer, InputStream is) {
        File file = new File(curDir, transfer.getFromFile().getName());
        FileOutputStream os = null;
        
        try {
            os = new FileOutputStream(file);
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }
        BufferedOutputStream bos = new BufferedOutputStream(os, BUFFERSIZE);
        byte[] buffer = new byte[BUFFERSIZE];
        int readCount = 0;
        try {
            while ((readCount = is.read(buffer)) != -1) {
                if (readCount < BUFFERSIZE) {
                    bos.write(buffer, 0, readCount);
                } else {
                    bos.write(buffer);
                }
                
            }
            is.close();
            bos.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        fireUpdate();
    }
    
    
    public void send(FileTransfer transfer, OutputStream os) {
        LocalFile file = cast(transfer.getFromFile());
        File fileObject = file.getFileObject();
        FileInputStream is = null;
        try {
            is = new FileInputStream(fileObject);
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }
        BufferedInputStream bis = new BufferedInputStream(is, BUFFERSIZE);
        byte[] buffer = new byte[BUFFERSIZE];
        int readCount = 0;
        System.out.println("Starting local send loop " + fileObject);
        try {
            while ((readCount = bis.read(buffer)) != -1) {
                if (readCount < BUFFERSIZE) {
                    os.write(buffer, 0, readCount);
                } else {
                    os.write(buffer);
                }
            }
            bis.close();
            os.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        
    }
    
    
    public void delete(Object object) {
        LocalFile file = cast(object);
        if(!file.getFileObject().delete()){
            System.out.println("could not delete "+file.getFileObject().getName());
        }
    }
    
    public boolean isAlwaysConnected() {
        return true;
    }

    public String getUserName() {
        return null;
    }

    public FileInterface getFileObjectFor(String fileName) {
        File file = new File(curDir.getAbsolutePath()+File.separatorChar+fileName);
        LocalFile localFile = new LocalFile(file, this);
        return localFile;
    }

    public Protocol getProtocol() {
        return Protocol.LOCAL;
    }

    protected void cd(String path) {
        curDir = new File(path);
    }

    public void mkdir(String name) {
        File newDir = new File(curDir,name);
        System.out.println("creating local folder: "+newDir.getAbsolutePath());
        boolean result = newDir.mkdir();
        System.out.println("result = "+result);
    }
    
    

    
}
